layui.use('table', function () {
    var table = layui.table;
    var layer = layui.layer;
    //展示已知数据
    table.render({
        elem: '#tableData' //指定table容器
        , id: 'TableData'
        , url: 'http://localhost:8081/competence/findAll' //数据接口
        , method: 'GET'
        , even: true //开启隔行背景
        , cols: [[ //表头
            {type: 'checkbox', align: 'center', value: 'competenceId',width: "15%"},
            {type: 'numbers', title: '序号', align: 'center',width: "15%"}, // 自增序号
            {field: 'competenceName', title: '权限名称',width: "30%"},
            {title: '操作',toolbar: '#tools', event: 'tool',width: "40%", fixed: 'right'}
        ]],
        toolbar: true,//开启自定义工具行
        defaultToolbar: ['filter', 'print', 'exports']
    });
	$("#addCompetence").click(function(){
		layer.confirm('请输入要添加的权限名称  <input type="text" id="competence" name="competence"  autocomplete="off" class="layui-input">', function (index) {
			$.ajax({
		        url: 'http://localhost:8081/competence/addCompetence',
		        type: "post",
		        data: {
		            competenceName: $("#competence").val()
		        }, success: function (data) {
		            if (data.code == 200) {
		                layer.msg('添加成功', {icon: 6, time: 1000});
		            } else {
		                layer.msg('添加失败', {icon: 5, time: 1000});
		            }
		        }, error: function (data) {
		            layer.msg(""+data.message+"", {icon: 5, time: 1000});
		        }
		    })
			setTimeout('location.reload();',1000);
		});
	})
    //监听行工具事件
    table.on('tool(test)', function (obj, index) {
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        if (layEvent === 'del') {
            layer.confirm(' 确定删除吗？', function (index) {
                obj.del();
                layer.close(index);
                $.ajax({
                    url: 'http://localhost:8081/competence/deleteCompetence',
                    type: "post",
                    data: {competenceId: obj.data.competenceId},
                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg('删除成功', {icon: 6, time: 1000});
                        } else {
                            layer.msg('删除失败', {icon: 5, time: 1000});
                        }
                    }, error: function (data) {
						layer.msg(""+data.message+"", {icon: 5, time: 1000});
                    }
                })
            });
        } else if (layEvent === "update") {
            var data = obj.data;
            layer.confirm('请输入权限名称  <input type="text" id="competence" name="competence"  autocomplete="off" class="layui-input">', function (index) {
				$.ajax({
                    url: 'http://localhost:8081/competence/updateCompetence',
                    type: "post",
                    data: {
                        competenceId: obj.data.competenceId,
                        competenceName: $("#competence").val()
                    }, success: function (data) {
                        if (data.code == 200) {
                            layer.msg('修改成功', {icon: 6, time: 1000});
                        } else {
                            layer.msg('修改失败', {icon: 5, time: 1000});
                        }
                    }, error: function (data) {
                        layer.msg(""+data.message+"", {icon: 5, time: 1000});
                    }
                })
				setTimeout('location.reload();',1000);
            });
        }
    })
});

$("#findCompetence").click(function(){
	reload();
})
function reload() {
    var table = layui.table;
	var competenceName = $("#competenceName").val()//权限名称
    //执行重载
    table.reload('TableData', {
        url: 'http://localhost:8081/competence/findByName',//数据接口
        method: "post",
        where: { //设定异步数据接口的额外参数，任意设
            competenceName: competenceName
        }
    });
}
//刷新
$("#reload").click(function(){
	location.reload()
})